﻿@using System;
@using Destiny.Core.Flow.CodeGenerator;


@{

    ProjectMetadata metadata = Model;
    string lowerFirstNmae = Destiny.Core.Flow.Extensions.Extensions.LowerFirstChar(metadata.EntityMetadata.EntityName);
    string repository = $"_{lowerFirstNmae}Repository";
}


using System;
using @(metadata.Namespace).Dtos;
using System.Threading.Tasks;
using Destiny.Core.Flow.Filter;
using Destiny.Core.Flow.Filter.Abstract;
using Destiny.Core.Flow.Ui;
using Destiny.Core.Flow.Extensions;
using Destiny.Core.Flow;
using Destiny.Core.Flow.Enums;
using Microsoft.Extensions.DependencyInjection;
using @(metadata.Namespace).Entities;
using @(metadata.Namespace).IServices.@(metadata.EntityMetadata.EntityName)s;


namespace @(metadata.Namespace).Services.@(metadata.EntityMetadata.EntityName)s
{
    /// &lt;summary&gt;
    ///@(string.IsNullOrEmpty(metadata.EntityMetadata.DisplayName) ? metadata.EntityMetadata.EntityName : metadata.EntityMetadata.DisplayName)
    /// &lt;/summary&gt;
    public class @(metadata.EntityMetadata.EntityName)Service : I@(metadata.EntityMetadata.EntityName)Service
    {

        private readonly IServiceProvider _provider;
        private readonly IEFCoreRepository&lt;@(metadata.EntityMetadata.EntityName), @(metadata.EntityMetadata.PrimaryKeyType)&gt; @repository;

        /// &lt;summary&gt;
        /// 初始化一个&lt;see cref="@(metadata.EntityMetadata.EntityName)Service"/&gt;类型的新实例
        /// &lt;/summary&gt;
        public @(metadata.EntityMetadata.EntityName)Service(IServiceProvider provider)
        {
            _provider = provider;
            @(repository) = provider.GetService&lt;IEFCoreRepository&lt;@(metadata.EntityMetadata.EntityName), @(metadata.EntityMetadata.PrimaryKeyType)&gt;&gt;();
        }
        
        /// &lt;summary&gt;
        /// 异步创建@(metadata.EntityMetadata.DisplayName)
        /// &lt;/summary&gt;
        /// &lt;param name="dto"&gt;添加的@(metadata.EntityMetadata.DisplayName)DTO&lt;/param&gt;
        public async Task&lt;OperationResponse&gt; CreateAsync(@(metadata.EntityMetadata.EntityName)InputDto dto)
        {
            return await @(repository).InsertAsync(dto);
        }
        
        
        /// &lt;summary&gt;
        /// 异步更新@(metadata.EntityMetadata.DisplayName)
        /// &lt;/summary&gt;
        /// &lt;param name="dto"&gt;更新的@(metadata.EntityMetadata.DisplayName)DTO&lt;/param&gt;
        public async Task&lt;OperationResponse&gt; UpdateAsync(@(metadata.EntityMetadata.EntityName)InputDto dto)
        {
            return await @(repository).UpdateAsync(dto);
        }

        /// &lt;summary&gt;
        /// 异步加载表单@(metadata.EntityMetadata.DisplayName)
        /// &lt;/summary&gt;
        /// &lt;param name="id"&gt;要加载的@(metadata.EntityMetadata.DisplayName)主键&lt;/param&gt;
        public async Task&lt;OperationResponse&lt;@(metadata.EntityMetadata.EntityName)OutputDto&gt;&gt; LoadFormAsync(@metadata.EntityMetadata.PrimaryKeyType id)
        {
            var dto = await @(repository).GetByIdToDtoAsync&lt;@(metadata.EntityMetadata.EntityName)OutputDto&gt;(id);
            return new OperationResponse&lt;@(metadata.EntityMetadata.EntityName)OutputDto&gt;("加载成功",dto,OperationResponseType.Success);
        }
        
        /// &lt;summary&gt;
        /// 异步删除@(metadata.EntityMetadata.DisplayName)
        /// &lt;/summary&gt;
        /// &lt;param name="id"&gt;要删除的@(metadata.EntityMetadata.DisplayName)主键&lt;/param&gt;
        public Task&lt;OperationResponse&gt; DeleteAsync(@metadata.EntityMetadata.PrimaryKeyType id)
        {
            id.NotEmpty(nameof(id));
            return @(repository).DeleteAsync(id);
        }
        
        /// &lt;summary&gt;
        /// 异步得到@(metadata.EntityMetadata.DisplayName)分页数据
        /// &lt;/summary&gt;
        /// &lt;param name="request"&gt;分页请求数据&lt;/param&gt;
        public Task&lt;IPagedResult&lt;@(metadata.EntityMetadata.EntityName)PageListDto&gt;&gt; GetPageAsync(PageRequest request)
        {
            return @(repository).Entities.ToPageAsync&lt; @(metadata.EntityMetadata.EntityName),@(metadata.EntityMetadata.EntityName)PageListDto&gt;(request);
        }
    }
}
